<?php


namespace Faye\controllers;


use Swoole\Coroutine\Http\Client;


class TestController
{
    public function index()
    {
        go(function () {
            $cli = new Client('httpbin.org', 80);
            $cli->get('/get');
            echo $cli->body;
            $cli->close();
        });

        go(function () {
            for ($i = 0; $i < 5; $i++) {
                echo "协程内部操作 $i \n";
                sleep(1); // 模拟耗时操作
            }
        });
        return msg(1);
    }

    public function slow_api()
    {
        sleep(3);
        return msg(1, "", [
            "n" => rand(1000, 9999),
            "t" => microtime(),
        ]);
    }
}